
/*
 * ReportUpdateServlet.java
 *
 * Created on 1 December, 2007, 5:18 PM
 */

import utility.MasterPLOperations;

import java.io.*;
import java.sql.ResultSet;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import database.RecordSearch;
import dbmanager.*;

/**
 *
 * @author Hemant
 * @version
 * This servlet is dedicated for handling update property request.
 * 
 */
public class UpdateServlet extends HttpServlet
{
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
    MasterPLOperations mpl;
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {
        System.out.println("I'm Update servlet");
        HttpSession session = request.getSession(true);
        
        String username = (String)session.getAttribute("username");
        String password = (String)session.getAttribute("password");
        String master = request.getParameter("mastername");
        
        Vector<Object> property_list = (Vector<Object>)session.getAttribute("property_list");
        System.out.println("Master is "+master);
        GetIdName get = new GetIdName();
        
        mpl = new MasterPLOperations(username, password);
        mpl.setMaster(master);
        Enumeration<Object> enum1 = request.getParameterNames();
        
        System.out.println(property_list);
        
        for(int i=0;i<property_list.size();i=i+2)
        {
        	//mpl.setOldUpdateProperty(master, (String)property_list.get(i), (String)property_list.get(i+1), (String)property_list.get(i+2));
        	mpl.setOldUpdateProperty(master, (String)property_list.get(i), (String)property_list.get(i+1));
        }
                
        try
        {
			while(enum1.hasMoreElements()) 
			{
				String param = (String) enum1.nextElement();
								
				if(!(param.equalsIgnoreCase("submit") || param.equalsIgnoreCase("reset")))
				{
					String param_val = request.getParameter(param);
					//String ref_val = request.getParameter(param+"_ref");
					//if(ref_val == null)
						//continue;
					System.out.println(param+"	"+param_val);
										
					//mpl.setUpdateProperty(master, param, param_val, ref_val);
					mpl.setUpdateProperty(master, param, param_val,"1");
					//break;
				}
			}
			
			mpl.updateProperty();			
			response.sendRedirect("Response");			
		}
        catch (Exception e)
		{
        	e.printStackTrace();
			get = null;
		}        
    }
    
    /** Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException 
    {
        doGet(request,response);
    }
    
}
